﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wf23_8HoistLamb
{
    public delegate void MyDlgt(bool b); //define namespace-level delegate  

    public partial class Form1 : Form
    {
        MyDlgt deStr;
        MyDlgt deMult;
        MyDlgt deStrLamb;
        MyDlgt deMultLamb;

        public Form1()
        {
            InitializeComponent();
            DefDeStr();
            DefDeMult();
            DefDeStrLamb();
            DefDeMultLamb();
        }//ctor.

        void DefDeStr()
        {
            string S = "1"; //S used in delegate's body--->HOISTING == S on heap.
            TxBApp.Text = S;
            deStr = delegate(bool b) //define delegate deStr via anonymous method:
            {
                if (b) S += "_" + (char)(S[S.Length - 1] + 2); //Compiler spots that S is used
                else S = "1";                           //in delegate. Places S on heap.
                TxBApp.Text = S;
            };
        }//.

        void DefDeMult()
        {
            int N = 5;
            LiBMult.Items[0] = N;     //Items[0] exists, as added in designer
            deMult = delegate(bool b) //define delegate deMult via anonymous method:
            {
                if (b)
                {
                    N *= 5;
                    LiBMult.Items.Add(N);
                }
                else
                {
                    N = 5;
                    LiBMult.Items.Clear();
                    LiBMult.Items.Add(N);
                }
            };
        }//.

        void DefDeStrLamb()
        {
            string S = "1";
            TxBAppLamb.Text = S;
            deStrLamb = x =>   //define delegate deStrLamb via lambda expression:
            {
                if (x == true) S += "_" + (char)(S[S.Length - 1] + 2);
                else S = "1";
                TxBAppLamb.Text = S;
            };
        }//.

        void DefDeMultLamb()
        {
            int N = 5;
            LiBMultLamb.Items[0] = N; //Items[0] exists, as added in designer
            deMultLamb = x => //define delegate deMultLamb via lambda expression:
            {
                if (x)
                {
                    N *= 5;
                    LiBMultLamb.Items.Add(N);
                }
                else
                {
                    N = 5;
                    LiBMultLamb.Items.Clear();
                    LiBMultLamb.Items.Add(N);
                }
            };
        }//.

        private void BApp_MouseDown(object sender, MouseEventArgs e)
        {
            deStr(e.Button != MouseButtons.Right);
        }//.

        private void BMult_MouseDown(object sender, MouseEventArgs e)
        {
            deMult(e.Button != MouseButtons.Right);
        }//.

        private void BAppLamb_MouseDown(object sender, MouseEventArgs e)
        {
            deStrLamb(e.Button != MouseButtons.Right);
        }//.

        private void BMultLamb_MouseDown(object sender, MouseEventArgs e)
        {
            deMultLamb(e.Button != MouseButtons.Right);
        }//.
    }
}
